/**
 * 
 */
package paleGoldenRod.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.Window;

/**
 * @author david
 *
 */
public class WordTrain extends Composite {
	private Label m_statusLabel = new Label("Initizliaing the game...");
	private Label m_resultLabel = new Label("Result: 0");
	private Label m_sentenceLabel = new Label("");
	private TextBox m_guessBox = new TextBox();
	private Button m_submitButton = new Button("submit");
	
	private final Integer m_teamid;
	private final int m_cpeople;
	private final String m_username;
	private Integer m_playerid = null;
	
	private final MessagingServiceAsync m_remoteService = GWTGUI.getMessagingService();
	
	/**
	 * 
	 */
	public WordTrain(int teamid, int cpeople, String username) {
		m_teamid = new Integer(teamid);
		m_cpeople = cpeople;
		m_username = username;
		
		// the panel that has everything
		VerticalPanel panel = new VerticalPanel();
		
		// status
		panel.add(m_statusLabel);
		
		// result label
		panel.add(m_resultLabel);
		
		// sentence label
		panel.add(m_sentenceLabel);
		
	    // submit button
		m_submitButton.addClickListener(new ClickListener() {
	      public void onClick(Widget sender)
	      {
	    	  onSubmit();
	      }
	    });
	    panel.add(m_submitButton);
	
	    // textbox
	    m_guessBox.addKeyboardListener(new KeyboardListenerAdapter() {
	    	public void onKeyDown(Widget sender, char keyCode, int modifiers)
	    	{
	    		// sumbit only if enter is pressed
	    		if (keyCode==KeyboardListener.KEY_ENTER) {
	    			onSubmit();
	    		}
	    	}
	    });
	    m_guessBox.setFocus(true);
		panel.add(m_guessBox);

		// tell server to initialize
		m_remoteService.wtJoinGame(m_teamid, m_cpeople, m_username, new GeneralCallback(GeneralCallback.INIT));
		
		// All composites must call initWidgem_remoteService.wtSendNewWord(m_teamid, newWord, callback);t() in their constructors.
		initWidget(panel);
		
		// Give the overall composite a style name.
		// setStyleName("example-OptionalCheckBox");
	}

	/**
	 * This is called when the user hits enter key or clicks submit
	 */
	private void onSubmit()
	{
		if (m_playerid == null)
			return;
		else if (m_guessBox.getText().length()==0) {
			m_remoteService.wtGetStatus(m_teamid, new GeneralCallback(GeneralCallback.UPDATE));
			return;
		}
		String newWord = m_guessBox.getText();
		m_sentenceLabel.setText(m_sentenceLabel.getText()+" "+newWord);
		// final MessagingServiceAsync m_remoteService = GWTGUI.getMessagingService();
		AsyncCallback callback = new SubmitCallback();
        m_remoteService.wtSendNewWord(m_teamid, newWord, callback);
		// Window.alert("How high?");
	}
	
	/**
	 * This class helps to communicate with server
	 */
	private final class SubmitCallback implements AsyncCallback {
		public SubmitCallback() {
		}
		
		public void onFailure(Throwable caught) {
			onRPCFail(caught);
		}
		
		public void onSuccess(Object result) {
			if (result==null) {
				Window.alert("resturns null");
			}
			else {
				final int numResult = ((Integer) result).intValue();
				m_resultLabel.setText("Result: "+numResult);
				if (numResult==0)
					Window.alert("You lost!");
			}
			// Window.alert("Result: "+numResult);
		}
	}
	
	private final class GeneralCallback implements AsyncCallback {
		public static final int INIT=0;
		public static final int UPDATE=1;
		public static final int LEAVE=3;
		private int m_type;
		
		public GeneralCallback(int type) {
			m_type=type;
		}
		
		public void onFailure(Throwable caught) {
			onRPCFail(caught);
		}
		
		public void onSuccess(Object result) {
			switch (m_type) {
			case INIT: {
				m_playerid=(Integer)result;
				// final int numResult = ((Integer) result).intValue();
				// m_resultLabel.setText("Result: "+numResult);
				break;
			}
			case UPDATE: {
				WTGameStatus status = (WTGameStatus)result;
				if (status.currentCount.intValue()==0)
					Window.alert("You won");
				// status.currentCount.intValue()
				break;
			// Window.alert("Result: "+numResult);
			}
			default:
			}
		}
	}

	private void onRPCFail(Throwable caught)
	{
		Window.alert("RPC Failed!\n" + caught.getMessage());
	}
}
